package com.snail.billing.page;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.net.HttpApp;
import com.android.net.HttpResult;
import com.android.net.HttpSession;
import com.android.net.OnHttpResultListener;
import com.android.util.AlertUtil;
import com.android.util.ResUtil;
import com.snail.billing.Resource;
import com.snail.billing.json.JsonBase;
import com.snail.billing.os.DialogPage;
import com.snail.billing.session.ForgetPwdQueryEmailSession;
import com.snail.billing.session.ForgetPwdQueryMobileSession;
import com.snail.billing.session.ForgetPwdSendEmailSession;
import com.snail.billing.session.ForgetPwdSendMoblieSession;

public class ForgetPage extends DialogPage implements View.OnClickListener, OnHttpResultListener {

	private TextView textTitle;
	private View buttonBack;

	private EditText inputAccount;
	private View buttonCaptcha;
	private View layout;
	//
	private View layoutMobile;
	private ImageView imageMobile;
	private TextView textMobile;
	//
	private View layoutEmail;
	private ImageView imageEmail;
	private TextView textEmail;
	//
	private View button;

	private HttpApp httpApp;
	private ForgetPwdQueryMobileSession forgetPwdQueryMobileSession;
	private ForgetPwdQueryEmailSession forgetPwdQueryEmailSession;

	private ForgetPwdSendMoblieSession forgetPwdSendMoblieSession;
	private ForgetPwdSendEmailSession forgetPwdSendEmailSession;

	private ForgetPwdQueryMobileSession.JsonData jsonForgetPwdQueryMobile;
	private ForgetPwdQueryEmailSession.JsonData jsonForgetPwdQueryEmail;

	private int state;

	private String accountString;

	@Override
	public int createLayoutId() {
		return ResUtil.getLayoutId(Resource.layout.snailbilling_forget_activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		textTitle = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_text));
		textTitle.setText(ResUtil.getString(Resource.string.snailbilling_forget_title));
		buttonBack = findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_button_back));
		buttonBack.setOnClickListener(this);
		//
		inputAccount = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_forget_input_account));
		buttonCaptcha = findViewById(ResUtil.getViewId(Resource.id.snailbilling_forget_button_captcha));
		layout = findViewById(ResUtil.getViewId(Resource.id.snailbilling_forget_layout));
		layoutMobile = findViewById(ResUtil.getViewId(Resource.id.snailbilling_forget_layout_moblie));
		imageMobile = (ImageView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_forget_image_mobile));
		textMobile = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_forget_text_mobile));
		layoutEmail = findViewById(ResUtil.getViewId(Resource.id.snailbilling_forget_layout_email));
		imageEmail = (ImageView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_forget_image_email));
		textEmail = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_forget_text_email));
		button = findViewById(ResUtil.getViewId(Resource.id.snailbilling_forget_button));
		buttonCaptcha.setOnClickListener(this);
		layoutMobile.setOnClickListener(this);
		layoutEmail.setOnClickListener(this);
		button.setOnClickListener(this);
		layout.setVisibility(View.INVISIBLE);
		//
		httpApp = new HttpApp(getContext());
		httpApp.setOnHttpResultListener(this);
	}

	private void showState(int state) {
		this.state = state;
		if (state == 0) {
			imageMobile.setImageResource(ResUtil.getDrawableId(Resource.drawable.snailbilling_check_on));
			textMobile.setVisibility(View.VISIBLE);
			imageEmail.setImageResource(ResUtil.getDrawableId(Resource.drawable.snailbilling_check_off));
			textEmail.setVisibility(View.INVISIBLE);
		} else if (state == 1) {
			imageMobile.setImageResource(ResUtil.getDrawableId(Resource.drawable.snailbilling_check_off));
			textMobile.setVisibility(View.INVISIBLE);
			imageEmail.setImageResource(ResUtil.getDrawableId(Resource.drawable.snailbilling_check_on));
			textEmail.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void onClick(View v) {
		if (v.equals(buttonBack)) {
			getPageManager().backward();
		} else if (v.equals(buttonCaptcha)) {
			accountString = inputAccount.getText().toString();
			if (TextUtils.isEmpty(accountString)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_forget_input_account));
				return;
			}
			forgetPwdQueryMobileSession = new ForgetPwdQueryMobileSession(accountString);
			httpApp.setDialogAutoDismiss(false);
			httpApp.request(forgetPwdQueryMobileSession);
		} else if (v.equals(layoutMobile)) {
			showState(0);
		} else if (v.equals(layoutEmail)) {
			showState(1);
		} else if (v.equals(button)) {
			if (state == 0) {
				forgetPwdSendMoblieSession = new ForgetPwdSendMoblieSession(accountString);
				httpApp.request(forgetPwdSendMoblieSession);
			} else if (state == 1) {
				forgetPwdSendEmailSession = new ForgetPwdSendEmailSession(accountString);
				httpApp.request(forgetPwdSendEmailSession);
			}
		}
	}

	@Override
	public void onHttpResult(HttpResult httpResult) {
		if (httpResult.isSucceed()) {
			HttpSession httpSession = httpResult.getHttpSession();
			if (httpSession.equals(forgetPwdQueryMobileSession)) {
				jsonForgetPwdQueryMobile = new ForgetPwdQueryMobileSession.JsonData((String) httpSession.getResponseData());
				if (jsonForgetPwdQueryMobile.getCode() == 1) {
					forgetPwdQueryEmailSession = new ForgetPwdQueryEmailSession(accountString);
					httpApp.request(forgetPwdQueryEmailSession);
				} else {
					httpApp.dialogDismiss();
					Toast.makeText(getContext(), jsonForgetPwdQueryMobile.getMessage(), Toast.LENGTH_SHORT).show();
				}
			} else if (httpSession.equals(forgetPwdQueryEmailSession)) {
				jsonForgetPwdQueryEmail = new ForgetPwdQueryEmailSession.JsonData((String) httpSession.getResponseData());
				if (jsonForgetPwdQueryEmail.getCode() == 1) {
					if (jsonForgetPwdQueryMobile.getBindState().equals("0") && jsonForgetPwdQueryEmail.getBindState().equals("0")) {
						Toast.makeText(getContext(), ResUtil.getString(Resource.string.snailbilling_forget_can_not_use), Toast.LENGTH_SHORT).show();
						return;
					}
					boolean isSelect = false;
					if (jsonForgetPwdQueryMobile.getBindState().equals("1")) {
						textMobile.setText(jsonForgetPwdQueryMobile.getMobile());
						isSelect = true;
						showState(0);
					} else {
						layoutMobile.setVisibility(View.GONE);
					}
					if (jsonForgetPwdQueryEmail.getBindState().equals("1")) {
						textEmail.setText(jsonForgetPwdQueryEmail.getEmail());
						if (!isSelect) {
							isSelect = true;
							showState(1);
						}
					} else {
						layoutEmail.setVisibility(View.GONE);
					}
					inputAccount.setEnabled(false);
					buttonCaptcha.setVisibility(View.INVISIBLE);
					layout.setVisibility(View.VISIBLE);
				} else {
					Toast.makeText(getContext(), jsonForgetPwdQueryMobile.getMessage(), Toast.LENGTH_SHORT).show();
				}
			} else if (httpSession.equals(forgetPwdSendMoblieSession)) {
				JsonBase jsonBase = new JsonBase((String) forgetPwdSendMoblieSession.getResponseData());
				//
				if (jsonBase.getCode() == 1) {
					Bundle args = new Bundle();
					args.putString("account", accountString);
					args.putInt("state", state);
					getPageManager().forward(ForgetPwdPage.class, args);
				} else {
					Toast.makeText(getContext(), jsonBase.getMessage(), Toast.LENGTH_SHORT).show();
				}
			} else if (httpSession.equals(forgetPwdSendEmailSession)) {
				JsonBase jsonBase = new JsonBase((String) forgetPwdSendEmailSession.getResponseData());
				//
				if (jsonBase.getCode() == 1) {
					Bundle args = new Bundle();
					args.putString("account", accountString);
					args.putInt("state", state);
					getPageManager().forward(ForgetPwdPage.class, args);
				} else {
					Toast.makeText(getContext(), jsonBase.getMessage(), Toast.LENGTH_SHORT).show();
				}
			}
		}
	}

}
